//
//  AnotherStarEvaluator.m
//  StarEvaluator
//
//  Created by Mac on 16/4/28.
//  Copyright © 2016年 jyb. All rights reserved.
//

#import "AnotherStarEvaluator.h"

@interface AnotherStarEvaluator ()
{
    float   aSpace; //间隙的宽度
    float   aWidth; //一个星星+间隙的宽度
    float   aStarWidth; //一个星星的宽度
    float   touchX;
    CGRect selfFrame;
    NSMutableArray  *fullStarArray;
}
@property (nonatomic, assign) float currentValue;

@end

@implementation AnotherStarEvaluator

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        selfFrame=frame;
        _currentValue = 0;
        self.backgroundColor = [UIColor clearColor];
        [self setNeedsDisplay];
    }
    return self;
}

- (void)setCurrentValue:(float)currentValue
{
    _currentValue = currentValue;
    CGFloat width = self.bounds.size.width / 5;
    aWidth = width;
    aStarWidth = width *0.7;
    aSpace = width *0.3;
    int integerNum=(int)currentValue;
    float decimalNum=currentValue-integerNum;
    touchX =(integerNum*aWidth+aStarWidth*decimalNum);
    [self setNeedsDisplay];
}


- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    int t = (int)(touchPoint.x/aWidth);
    float f = (touchPoint.x - t*aSpace - t*aStarWidth)/aStarWidth;
    if (f>=0.5) {
        f=1.0;
    }else if(f>=0){
        f=0.5;
    }else if(f<0){
        f=0;
    }
    self.currentValue = t + f;
     touchX =(t*aWidth+aStarWidth*f);
    if ([self.delegate respondsToSelector:@selector(anotherStarEvaluatorIshaveChange:)]) {
        [self.delegate anotherStarEvaluatorIshaveChange:self];
    }
//    touchX = touchPoint.x;
    [self setNeedsDisplay];
    
    return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    int t = (int)(touchPoint.x/aWidth);
    float f = (touchPoint.x - t*aSpace - t*aStarWidth)/aStarWidth;
    if (f>=0.5) {
        f=1.0;
    }else if(f>=0){
        f=0.5;
    }else if(f<0){
        f=0;
    }
    self.currentValue = t + f;
    
    touchX =(t*aWidth+aStarWidth*f);   // touchPoint.x;
    [self setNeedsDisplay];
    if ([self.delegate respondsToSelector:@selector(anotherStarEvaluatorIshaveChange:)]) {
        [self.delegate anotherStarEvaluatorIshaveChange:self];
    }
    return YES;
}



- (void)drawRect:(CGRect)rect {
//    CGFloat width = self.bounds.size.width / 5;
//    aWidth = width;
//    aStarWidth = width *0.6;
//    aSpace = width *0.4;
    UIImage *image = [UIImage imageNamed:@"eva_bigStar"];
    for (int i = 0; i < 5; i ++) {
        CGRect rect = CGRectMake(i*(aStarWidth+aSpace), (selfFrame.size.height-aStarWidth)/2, aStarWidth, aStarWidth);
        [image drawInRect:rect];
    }
    
    [[UIColor lightGrayColor] set];
    UIRectFillUsingBlendMode(rect, kCGBlendModeSourceIn);
    
    CGRect newRect = CGRectMake(0, 0, touchX, rect.size.height);
    [[UIColor orangeColor] set];
    UIRectFillUsingBlendMode(newRect, kCGBlendModeSourceIn);
    
    if ([_delegate respondsToSelector:@selector(anotherStarEvaluator:currentValue:)]) {
        if(_currentValue<=0){
            _currentValue=0;
        }
        [_delegate anotherStarEvaluator:self currentValue:_currentValue*2];
    }
}

@end
